package com.study.service.Impl;

import com.study.constant.CouponConstant;
import com.study.context.BaseContext;
import com.study.dto.CouponDTO;
import com.study.entity.Coupon;
import com.study.mapper.CouponMapper;
import com.study.service.CouponService;
import com.study.vo.CouponVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    /**
     * 根据用户id和优惠券状态获取优惠券
     *
     * @param couponDTO
     * @return
     */
    @Override
    public List<CouponVO> getList(CouponDTO couponDTO) {
        //获取当前线程用户id
        Long userId = BaseContext.getCurrentId();
        if (couponDTO.getUserId() == null || !couponDTO.getUserId().equals(userId)) {
            throw new IllegalArgumentException("非法用户id，优惠券获取失败");
        }

        List<CouponVO> couponList = couponMapper.getList(couponDTO);
        return couponList;
    }

    /**
     * 用户领取优惠券
     *
     * @param couponId
     */
    @Override
    public void userGetCoupon(Long couponId) {
        //查询该优惠券是否存在，不存在抛出异常
        Coupon coupon = couponMapper.getCouponById(couponId);
        if (coupon == null) {
            throw new IllegalArgumentException("优惠券不存在");
        }

        //查询用户是否已经领取过该优惠券
        Long userId = BaseContext.getCurrentId();
        coupon = couponMapper.getCouponByUserIdAndCouponId(userId, couponId,null);
        //领取过，抛出异常
        if (coupon != null) {
            throw new IllegalArgumentException("优惠券已领取");
        }
        //未领取
        //计算优惠券过期时间
        LocalDate expireTime = LocalDate.now().plusDays(CouponConstant.EXPIRE_TIME_ONE);
        LocalTime time = LocalTime.of(23, 59, 59);

        LocalDateTime from = LocalDateTime.of(expireTime, time);
        //将优惠券信息插入用户优惠券表
        couponMapper.userGetCoupon(userId, couponId, from);
    }

    /**
     * 查询所有可用优惠券
     *
     * @return
     */
    @Override
    public List<Coupon> getAllCoupon() {
        List<Coupon> couponList = couponMapper.getAllCoupon();
        return couponList;
    }

    /**
     * 根据条件查询优惠券
     *
     * @param couponDTO
     * @return
     */
    @Override
    public List<Coupon> getListByCondition(CouponDTO couponDTO) {
        Map map = new HashMap<>();
        map.put("tag", couponDTO.getTag());
        map.put("type", couponDTO.getType());
        map.put("isExpired", couponDTO.getIsExpired());
        List<Coupon> couponList = couponMapper.getListByMap(map);
        return couponList;
    }

    /**
     * 根据用户id获取用户优惠券id列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> getUserCouponIdList(Long userId) {
        //获取当前线程用户id
        Long currentId = BaseContext.getCurrentId();
        if (userId == null || !userId.equals(currentId)) {
            log.error("非法用户id，获取用户优惠券id列表失败");
            throw new IllegalArgumentException("非法用户id，获取失败");
        }

        List<Long> couponIdList = couponMapper.getUserCouponIdList(userId);

        return couponIdList;
    }
}
